<template>
  <div class="space-usage-analyze">
    <a-flex gap="middle">
      <a-card title="空间使用情况分析" style="width: 50%">
        <div style="text-align: center; height: 320px">
          <h3>
            {{ formatSize(data.usedSize) }} /
            {{ data.maxSize ? formatSize(data.maxSize) : '无限制' }}
          </h3>
          <a-progress type="dashboard" :percent="data.sizeUsageRatio ?? 0" />
        </div>
      </a-card>
      <a-card title="图片数量情况分析" style="width: 50%">
        <div style="text-align: center; height: 320px">
          <h3>
            {{ data.usedCount }} /
            {{ data.maxCount ?? '无限制' }}
          </h3>
          <a-progress type="dashboard" :percent="data.countUsageRatio ?? 0" />
        </div>
      </a-card>
    </a-flex>
  </div>
</template>

<script setup lang="ts">
import 'echarts'
import { ref, watchEffect } from 'vue'
import { getSpaceUsageAnalyzeUsingPost } from '@/apis/SpaceAnalyzeInterface.ts'
import { message } from 'ant-design-vue'
import { formatSize } from '../../utils'

// 定义需要向后端发送的数据
interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})

// 定义加载状态
const loading = ref(false)
// 从后端获取数据
const data = ref({})
// 获取数据
const fetchData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
  }
  const res = await getSpaceUsageAnalyzeUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    data.value = res.data.data
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  loading.value = false
}

// 监听事件钩子，改变时触发数据重新加载
watchEffect(() => {
  fetchData()
})
</script>

<style scoped>
.space-usage-analyze {
}
</style>
